package net.tycmc.cty.ctyms.timejob.common;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;

public class DynamicReadConfig {

	/**
	 * 获取配置文件的属性
	 * @param configPath	配置文件路径
	 * @param propertyName	要获取该属性的值
	 * @return	读取的属性值
	 * @throws ConfigurationException 异常向上抛出，有调用该方法的用户进行处理
	 */
	public static String getConfigProperty(String configPath,String propertyName) throws ConfigurationException{
		
		Configuration config = new PropertiesConfiguration(configPath);
		String rs = config.getString(propertyName);
		
		return rs;
	}
	
	/**
	 * 获取日志文件存放路径
	 * @return
	 */
	public static String getLogPath(){
		String value = "";
		try {
			value = DynamicReadConfig.getConfigProperty("log4j.properties", "log4j.appender.R.File");
		} catch (ConfigurationException e) {
			e.printStackTrace();
		}
		return value.substring(0,value.lastIndexOf("/"));
	}
	
	/**
	 * 通过文件夹路径获取文件夹下的文件名称
	 * @param path
	 * @return
	 */
	public static List<Map<String,String>> getFileNameList(String path) {
		File file = new File(path);
		File[] array = file.listFiles();
		List<Map<String,String>> list = new ArrayList<Map<String,String>>();
		for (int i = 0; i < array.length; i++) {
			if (array[i].isFile()) {
				Map<String,String> map = new HashMap<String,String>();
				map.put("name", array[i].getName());
				map.put("path", array[i].getPath());
				list.add(map);
			} else if (array[i].isDirectory()) {
				getFileNameList(array[i].getPath());
			}
		}
		return list;
	}
}
